package com.xfatm.killer.entity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xfatm.killer.R;
import com.xfatm.killer.task.DisableAllAppTask;
import com.xfatm.killer.task.EnableAllAppTask;
import com.xfatm.killer.ui.AboutActivity;
import com.xfatm.killer.ui.HelpActivity;
import com.xfatm.killer.ui.SettingActivity;

/**
 * Created by ASUS on 2018/1/27.
 */

public class TabFragment extends Fragment {

    public MainTabEnum mainTabEnum;
    private View view;

    public static TabFragment newInstance(MainTabEnum tabTitle) {
        Bundle args = new Bundle();
        TabFragment fragment = new TabFragment();
        fragment.mainTabEnum=tabTitle;
        args.putString("title",tabTitle.getTitle());
        args.putString("mainTabEnum",tabTitle.toString());
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        //if (getArguments() != null) tv.setText(getArguments().getString(TITLE_TAG));

        if(view==null){
            if(mainTabEnum==null){
                mainTabEnum=MainTabEnum.getEnumByName(getArguments().getString("mainTabEnum"));
            }
            Log.i("sout","初始化"+mainTabEnum+"value="+getArguments().getString("title"));
            if(mainTabEnum==MainTabEnum.ALL_APP){
                view = inflater.inflate(R.layout.all_app_list, container, false);
                ListView listView=view.findViewById(R.id.allAppList);
                AppInfoAdapter appInfoAdapter=new AppInfoAdapter(listView.getContext(),AppInfoController.getInstance().getNormalApps());
                listView.setAdapter(appInfoAdapter);
                listView.setOnItemClickListener(appInfoAdapter);
            }else if(mainTabEnum==MainTabEnum.SYSTEM_APP){
                view = inflater.inflate(R.layout.system_app_list, container, false);
                ListView listView=view.findViewById(R.id.systemAppList);
                AppInfoAdapter appInfoAdapter=new AppInfoAdapter(listView.getContext(),AppInfoController.getInstance().getSysApps());
                listView.setAdapter(appInfoAdapter);
                listView.setOnItemClickListener(appInfoAdapter);
            }else if(mainTabEnum==MainTabEnum.SELECT){
                view = inflater.inflate(R.layout.select_app_grid, container, false);
                GridView gridView=view.findViewById(R.id.selectAppGrid);
                SelectAppInfoAdapter selectAppInfoAdapter=new SelectAppInfoAdapter(gridView.getContext(),AppInfoController.getInstance().getSelectApps());
                gridView.setAdapter(selectAppInfoAdapter);
                gridView.setOnItemClickListener(selectAppInfoAdapter);
                AppInfoController.getInstance().setSelectAppInfoAdapter(selectAppInfoAdapter);
                //禁用按钮点击事件
                view.findViewById(R.id.disableAll).setOnClickListener(new DisableAllAppClick());
                //长按事件
                gridView.setOnItemLongClickListener(selectAppInfoAdapter);
            }else if(mainTabEnum==MainTabEnum.OTHER){
                view = inflater.inflate(R.layout.other_opera_layout, container, false);
                initOtherOperButton(view);
            }else{
                view=new TextView(container.getContext());
            }
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("sout","resume"+mainTabEnum);
    }

    private class DisableAllAppClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            TextView textView=v.findViewById(R.id.disableText);
            ProgressBar progressBar=v.findViewById(R.id.disableProgress);
            if(progressBar.getVisibility()==View.VISIBLE){
                return;
            }
            textView.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
            new DisableAllAppTask(v).execute();
        }
    }

    //初始化其他操作的几个按钮
    private void initOtherOperButton(final View view){
       //帮助
        view.findViewById(R.id.helpBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), HelpActivity.class);
                view.getContext().startActivity(intent);
            }
        });

        //关于
        view.findViewById(R.id.aboutBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), AboutActivity.class);
                view.getContext().startActivity(intent);
            }
        });

        //设置
        view.findViewById(R.id.settingBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(v.getContext(), SettingActivity.class);
                view.getContext().startActivity(intent);
            }
        });

        //启用所有
        view.findViewById(R.id.enableAll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog=new Dialog(v.getContext(),R.style.loading_dialog);
                dialog.setContentView(R.layout.load_dialog);
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                new EnableAllAppTask(dialog).execute();
            }
        });
    }
}